package com.liyuhan.onlinetaxibackground.Service.impl;

import com.google.gson.Gson;
import com.liyuhan.onlinetaxibackground.POJO.*;
import com.liyuhan.onlinetaxibackground.Repository.BasicOrderRepository;
import com.liyuhan.onlinetaxibackground.Repository.CompletedOrderRepository;
import com.liyuhan.onlinetaxibackground.Repository.ReceivedOrderRepository;
import com.liyuhan.onlinetaxibackground.Service.ReceivedOrderService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import com.liyuhan.onlinetaxibackground.dto.orderDto.CompletedOrderDto;
import com.liyuhan.onlinetaxibackground.dto.orderDto.ReceivedOrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ReceivedOrderImpl implements ReceivedOrderService {
    @Autowired
    private ReceivedOrderRepository receivedOrderRepository;

    @Autowired
    private BasicOrderRepository basicOrderRepository;

    @Autowired
    private CompletedOrderRepository completedOrderRepository;

    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse getReceiverOrder(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        Integer orderId = Integer.valueOf(requestBody.getData().get("orderId").toString());

        // 根据id获取订单
        BasicOrder order = basicOrderRepository.findById(orderId).get();

        map.clear();

        // 获取接单信息
        ReceivedOrder receivedOrder = receivedOrderRepository.findByOrder(order).get();
        // 获取司机信息
        Driver driver = receivedOrder.getDriver();
        Car car = driver.getCar();
        // 返回订单
        // 将basicOrder转化为orderDTO
        ReceivedOrderDto receivedOrderDto = new ReceivedOrderDto(
                receivedOrder.getId(),
                order,
                receivedOrder.getState(),
                driver.getCity(),
                driver.getAddress(),
                driver.getUser().getScore(),
                car.getCarBrand(),
                car.getCarColor(),
                car.getCarNumber(),
                receivedOrder.getPickUpTime(),
                receivedOrder.getCancelOrder(),
                receivedOrder.getCancelReason()
        );

        // 将orderDTO转化为GSON字符串
        Gson gson = new Gson();
        String orderStr = gson.toJson(receivedOrderDto);

        // 将字符串放入map
        map.put("order", orderStr);

        return new DataResponse("200", map);
    }

    @Override
    public DataResponse verifyPhone_4(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        map.clear();

        String phone_4 = requestBody.getData().get("phone_4").toString();
        String orderId = requestBody.getData().get("orderId").toString();

        BasicOrder order = basicOrderRepository.findById(Integer.valueOf(orderId)).get();

        String phone = order.getPassenger().getUser().getPhone();
        if (phone_4.equals(phone.substring(phone.length() - 4, phone.length()))) {
            return new DataResponse("200", null);
        } else {
            map.put("error", "您输入的手机号与乘客手机号后四位不匹配！");

            return new DataResponse("403", map);
        }

    }

    @Override
    public void cancelOrderRequest(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        // 撤销的订单号
        Integer orderId = Integer.valueOf(requestBody.getData().get("orderId").toString());
        // 撤销原因
        String cancelReason = requestBody.getData().get("cancelReason").toString();

        // 获取订单
        ReceivedOrder order = receivedOrderRepository.findById(orderId).get();

        // 设置撤销请求位
        order.setCancelOrder("1");
        order.setCancelReason(cancelReason);

        // 更新订单状态
        receivedOrderRepository.save(order);
    }

    @Override
    public void goingToDestination(String request) {
        // 前往目的地
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String orderId = requestBody.getData().get("orderId").toString();

        // 获得订单
        ReceivedOrder receivedOrder = receivedOrderRepository.findById(Integer.valueOf(orderId)).get();

        // 修改订单状态
        receivedOrder.setState("4");

        // 添加用户上车时间
        receivedOrder.setPickUpTime(new Date());

        // 保存订单
        receivedOrderRepository.save(receivedOrder);
    }

    // 根据用户传入的订单id，获取订单状态
    @Override
    public DataResponse getOrderState(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        Integer orderId = Integer.valueOf(requestBody.getData().get("orderId").toString());

        // 根据id获取订单
        ReceivedOrder order = receivedOrderRepository.findById(orderId).get();

        map = new HashMap<>();
        map.put("state", order.getState());

        // 将订单状态回传
        return new DataResponse("200", map);
    }

    @Override
    public DataResponse completeOrder(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String orderId = requestBody.getData().get("orderId").toString();

        ReceivedOrder receivedOrder = receivedOrderRepository.findById(Integer.valueOf(orderId)).get();

        // 设置状态为订单完成
        receivedOrder.setState("6");

        receivedOrderRepository.save(receivedOrder);

        // 创建完成订单
        CompletedOrder completedOrder = new CompletedOrder("7", receivedOrder, receivedOrder.getPassenger(), receivedOrder.getDriver());

        completedOrder.setArriveTime(new Date());
        // 保存完成订单
        completedOrderRepository.save(completedOrder);

        // 不设置返回体
        return new DataResponse("200", null);
    }

    @Override
    public DataResponse isCanceled(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);
        map.clear();

        String orderId = requestBody.getData().get("orderId").toString();

        ReceivedOrder receivedOrder = receivedOrderRepository.findById(Integer.valueOf(orderId)).get();

        if (receivedOrder.getCancelOrder().equals("0")) {
            // 用户没有取消
            return new DataResponse("200", map);
        }

        if (receivedOrder.getCancelOrder().equals("1")) {
            // 用户取消订单，返回取消原因
            map.put("cancelReason", receivedOrder.getCancelReason());

            return new DataResponse("200", map);
        }

        if (receivedOrder.getCancelOrder().equals("3")) {
            // 司机拒绝撤销，乘客可以再次提出撤销
            return new DataResponse("200", map);
        }

        return new DataResponse("1000", null);
    }

    @Override
    public DataResponse setDriverCancel(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String orderId = requestBody.getData().get("orderId").toString();
        String cancelState = requestBody.getData().get("cancelState").toString();

        ReceivedOrder receivedOrder = receivedOrderRepository.findById(Integer.valueOf(orderId)).get();

        if (cancelState.equals("2")) {
            receivedOrder.setCancelOrder("2");
        } else if (cancelState.equals("3")) {
            receivedOrder.setCancelOrder("3");
        }

        receivedOrderRepository.save(receivedOrder);

        return new DataResponse("200", null);
    }

    @Override
    public DataResponse isAgree(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);
        map.clear();

        String orderId = requestBody.getData().get("orderId").toString();

        ReceivedOrder receivedOrder = receivedOrderRepository.findById(Integer.valueOf(orderId)).get();

        if (receivedOrder.getCancelOrder().equals("2")) {
            // 司机同意撤销
            map.put("cancelState", "2");
        } else if (receivedOrder.getCancelOrder().equals("3")) {
            // 司机拒绝撤销
            map.put("cancelState", "3");
        }

        return new DataResponse("200", map);
    }
}
